// ====================
// 顶层程序设计
// ====================
module main(
	input 		CLK_50M,				// 系统时钟信号50MHz
	input		Rst_n,					// 复位
	input 		[7:1] BUT,				// 按键值
	output	 	[8:1] LED,				// LED灯
	output		[6 : 1]	SEG_NCS,		// 6个数码管的片选（LOW），LSB对应最左边
	output		[7 : 0]	SEG_LED			// 共阳，bit0点、1中间、2左上、3左下、4下、5右下、6右上、7上
	);
	

localparam			TIME8MS_CNT = 200000;
reg					clk_8ms;			// 使用clk_8m处理
integer				clk_cnt;

wire [7:1] key;
wire [7:0] x, y;
wire [15:0] cout;

// =================================
// 得到周期为8ms的时钟
// =================================
always @(posedge CLK_50M or negedge Rst_n)
begin
	if(!Rst_n) begin 
		clk_8ms <= 1'b0;
		clk_cnt <= 0;
	end
	else if(clk_cnt == TIME8MS_CNT-1) begin
		clk_cnt <= 0;
		clk_8ms <= ~clk_8ms;
	end
	else clk_cnt <= clk_cnt + 1;
end

// =================================
// 数码管动态显示模块
// =================================
Seg_Lut Seg_u1(
	.CLK_50M			(CLK_50M),
	.Rst_n				(Rst_n),
	.BUT				(key[7:1]),
	.x					(x),
	.y					(y),
	.cout				(cout),
	.seg_nCS			(SEG_NCS),
	.seg_leds			(SEG_LED),
	.led				(LED)
);



// =================================
// 8位乘法器模块
// =================================
Multi_8bits Multi_8bits_U1(
	.CLK_50M			(CLK_50M),
	.rst_n				(Rst_n),
	.key				(key[5:2]),
	.x					(x),
	.y					(y),
	.cout				(cout),
);

// =================================
// 调用按键消抖模块
// =================================
key_debounce key_debounce_u1(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[1]),
	.key_value			(key[1])
);
key_debounce key_debounce_u2(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[2]),
	.key_value			(key[2])
);
key_debounce key_debounce_u3(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[3]),
	.key_value			(key[3])
);
key_debounce key_debounce_u4(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[4]),
	.key_value			(key[4])
);
key_debounce key_debounce_u5(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[5]),
	.key_value			(key[5])
);
key_debounce key_debounce_u6(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[6]),
	.key_value			(key[6])
);
key_debounce key_debounce_u7(
	.sys_clk			(CLK_50M),
	.sys_rst_n			(Rst_n),
	.key				(BUT[7]),
	.key_value			(key[7])
);


endmodule

